#include<iostream>
#include<queue>
using namespace std;
typedef long long LL;
priority_queue<int>q;
int n;
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x;
		cin >> x;
		q.push(x);
	}
	LL ans = 0;
	while (q.size() != 1) {
		int k1 = q.top();
		q.pop();
		int k2 = q.top();
		q.pop();
		ans += k1 * k2;
		q.push(k1 + k2);
	}
	cout << ans << endl;
	return 0;
}